import time

import allure
from selenium.webdriver.common.by import By

from base.base_driver import BaseDriver
from pages.contact_page import ContactPage
from utils.log_utils import logger


class AddPersonPage(BaseDriver):
    _NAME_INPUT = By.ID, "username"
    _UUID_INPUT = By.ID, "memberAdd_acctid"
    _PHONE_NUMBER_INPUT = By.ID, "memberAdd_phone"
    _PHONE_NUMBER_MSG = By.CSS_SELECTOR, ".ww_telInput+.ww_inputWithTips_tips"
    _INVITE_BTN = By.XPATH, "//*[text()='保存后自动发送邀请通知']"
    _SAVE_BTN = By.CSS_SELECTOR, ".js_btn_save"
    _NAME_LIST = By.XPATH, "//*[@data-type='member']/td[2]"
    _TOAST_TEXT = By.CSS_SELECTOR, '.ww_telInput+div'
    _NAME_MSG = By.CSS_SELECTOR, "#username+.ww_inputWithTips_tips"
    _BLANK_CLICK = By.XPATH, "//*[text()='添加成员' and @class='ww_commonCntHead_title_inner_text']"
    _ALIAS_INPUT = By.ID, "memberAdd_english_name"
    _ALIAS_MSG = By.CSS_SELECTOR, "#memberAdd_english_name+.ww_inputWithTips_tips"
    _UUID_MSG = By.CSS_SELECTOR, "#memberAdd_acctid+.ww_inputWithTips_tips"
    _WORK_EMAIL_INPUT =By.ID, "memberAdd_biz_mail"
    _WORK_EMAIL_MSG = By.CSS_SELECTOR, ".memberAdd_biz_mail_suffix+.ww_inputWithTips_tips"
    _FIXED_PHONE_INPUT = By.ID, "memberAdd_telephone"
    _FIXED_PHONE_MSG = By.CSS_SELECTOR, "#memberAdd_telephone+.ww_inputWithTips_tips"
    _ADDRESS_INPUT = By.ID, "memberEdit_address"
    _ADDRESS_MSG = By.CSS_SELECTOR, "#memberEdit_address+.ww_inputWithTips_tips"
    _TITLE_INPUT = By.ID, "memberAdd_title"
    _TITLE_MSG = By.CSS_SELECTOR, "#memberAdd_title+.ww_inputWithTips_tips"
    _EXTERN_POSITION_BTN = By.XPATH, "//*[text()='自定义 ']"
    _EXTERN_POSITION_INPUT = By.CSS_SELECTOR, "[name='extern_position']"
    _EXTERN_POSITION_MSG = By.CSS_SELECTOR, "[name='extern_position']+.ww_inputWithTips_tips"
    _BACK_BTN = By.XPATH, "//*[text()='离开此页']"

    def input_name(self, name):
        # logger.info('输入姓名')
        with allure.step('输入姓名'):
            # 定位姓名输入框并输入姓名
            self.find_and_input(*self._NAME_INPUT, name)
        # 返回到类自身
        return self

    def input_name_msg(self):
        # logger.info('点击空白处')
        with allure.step('点击空白处'):
            self.find_and_click(*self._BLANK_CLICK)
        # logger.info('获取姓名输入框提示信息')
        with allure.step('获取姓名输入框提示信息'):
            text = self.get_ele_test(By.CSS_SELECTOR, "#username+.ww_inputWithTips_tips")
        return text

    def input_alias(self, alias):
        # logger.info('输入别名')
        # 定位别名输入框并输入别名
        with allure.step('输入别名'):
            self.find_and_input(*self._ALIAS_INPUT, alias)
        # 返回到类自身
        return self

    def input_alias_msg(self):
        # logger.info('点击空白处')
        with allure.step('点击空白处'):
            self.find_and_click(*self._BLANK_CLICK)
        # logger.info('获取别名输入框提示信息')
        with allure.step('获取别名输入框提示信息'):
            text = self.get_ele_test(*self._ALIAS_MSG)
        return text

    def input_uuid(self, uuid):
        # logger.info('输入账号')
        # 定位账号输入框并输入账号
        with allure.step('输入账号'):
            self.find_and_input(*self._UUID_INPUT, uuid)
        # 返回到类自身
        return self

    def input_uuid_msg(self):
        # logger.info('点击空白处')
        with allure.step('点击空白处'):
            self.find_and_click(*self._BLANK_CLICK)
        # logger.info('获取账号输入框提示信息')
        with allure.step('获取账号输入框提示信息'):
            text = self.get_ele_test(*self._UUID_MSG)
        return text

    def input_work_email(self, uuid):
        # logger.info('输入企业邮箱')
        with allure.step('输入企业邮箱'):
        # 定位企业邮箱输入框并输入
            self.find_and_input(*self._WORK_EMAIL_INPUT, uuid)
        # 返回到类自身
        return self

    def input_work_email_msg(self):
        # logger.info('点击空白处')
        with allure.step('点击空白处'):
            self.find_and_click(*self._BLANK_CLICK)
        # logger.info('获取企业邮箱输入框提示信息')
        with allure.step('获取企业邮箱输入框提示信息'):
            text = self.get_ele_test(*self._WORK_EMAIL_MSG)
        return text

    def input_phone_number(self, phone):
        # logger.info('输入手机号')
        with allure.step('输入手机号'):
        # 定位手机号输入框并输入手机号
            self.find_and_input(*self._PHONE_NUMBER_INPUT, phone)
        return self

    def input_phone_number_msg(self):
        # logger.info('点击空白处')
        with allure.step('点击空白处'):
            self.find_and_click(*self._BLANK_CLICK)
        # logger.info('获取手机号输入框提示信息')
        with allure.step('获取手机号输入框提示信息'):
            text = self.get_ele_test(*self._PHONE_NUMBER_MSG)
        return text

    def input_fixed_phone(self, fixed_phone):
        # logger.info('输入座机号')
        # 定位座机输入框并输入座机号
        with allure.step('输入座机号'):
            self.find_and_input(*self._FIXED_PHONE_INPUT, fixed_phone)
        return self

    def input_fixed_phone_msg(self):
        # logger.info('点击空白处')
        with allure.step('点击空白处'):
            self.find_and_click(*self._BLANK_CLICK)
        # logger.info('获取座机输入框提示信息')
        with allure.step('获取座机输入框提示信息'):
            text = self.get_ele_test(*self._FIXED_PHONE_MSG)
        return text

    def input_address(self, address):
        # logger.info('输入地址')
        # 定位地址输入框并输入地址
        with allure.step('输入地址'):
            self.find_and_input(*self._ADDRESS_INPUT, address)
        return self

    def input_address_msg(self):
        # logger.info('点击空白处')
        with allure.step('点击空白处'):
            self.find_and_click(*self._BLANK_CLICK)
        logger.info('获取地址输入框提示信息')
        with allure.step('获取地址输入框提示信息'):
            text = self.get_ele_test(*self._ADDRESS_MSG)
        return text

    def input_title(self, title):
        # logger.info('输入职务')
        with allure.step('输入职务'):
        # 定位职务输入框并输入职务
            self.find_and_input(*self._TITLE_INPUT, title)
        return self

    def input_title_msg(self):
        # logger.info('点击空白处')
        with allure.step('点击空白处'):
            self.find_and_click(*self._BLANK_CLICK)
        # logger.info('获取职务输入框提示信息')
        with allure.step('获取职务输入框提示信息'):
            text = self.get_ele_test(*self._TITLE_MSG)
        return text

    def input_extern_position(self, extern_position):
        # logger.info('输入自定义职务')
        # 点击职务自定义单选框
        with allure.step('点击职务自定义单选框'):
            self.find_and_click(*self._EXTERN_POSITION_BTN)
        # 定位自定义职务输入框并输入自定义职务
        self.wait_elements(self._EXTERN_POSITION_INPUT)
        with allure.step('输入自定义职务'):
            self.find_and_input(*self._EXTERN_POSITION_INPUT, extern_position)
        return self

    def input_extern_position_msg(self):
        # logger.info('点击空白处')
        with allure.step('点击空白处'):
            self.find_and_click(*self._BLANK_CLICK)
        # logger.info('获取自定义职务输入框提示信息')
        with allure.step('获取自定义职务输入框提示信息'):
            text = self.get_ele_test(*self._EXTERN_POSITION_MSG)
        return text


    def click_invite(self):
        logger.info('取消勾选保存自动发送邀请通知')
        # 定位并点击“保存后自动发送邀请通知”,取消勾选
        self.find_and_click(*self._INVITE_BTN)
        return self

    def click_save(self):
        # logger.info('点击保存按钮')
        with allure.step('点击保存按钮'):
        # 定位并点击保存按钮
            self.find_and_click(*self._SAVE_BTN)
        # 调用封装的显式等待 获取通讯录页面的姓名列表
        with allure.step('获取通讯录页面的姓名列表'):
            self.wait_elements(self._NAME_LIST, 30)
        return ContactPage(self.driver)

    def click_save_same_phone(self):
        # logger.info('点击保存按钮')
        with allure.step('点击保存按钮'):
            self.find_and_click(*self._SAVE_BTN)
        # 等待提示信息出现
        time.sleep(2)
        with allure.step('获取提示文本信息'):
            text = self.get_ele_test(*self._TOAST_TEXT)
        return text

    def goto_main_page(self):
        self.back()
        if len(self.find_eles(*self._BACK_BTN)) >=1:
            self.find_and_click(*self._BACK_BTN)
        from pages.main_page import MainPage
        return MainPage(self.driver)
